package co.fitstart.admin.dao;

import java.util.Date;
import java.util.List;

import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;

import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.coupon.CouponStatus;
import co.fitstart.entity.coupon.DiscountType;
import co.fitstart.entity.market.satisfaction.SatisfactionSurvey;
import co.fitstart.entity.site.Site;
import co.fitstart.entity.user.Student;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Dynamic("hibernate")
public interface SatisfactionSurveyDao {

    /**
     * 有尚未完成问卷或最近两个月有做过问卷的学员
     * @param fillInTime 两个月前
     * @return
     */
    @Query("SELECT survey.courseRound.student FROM SatisfactionSurvey survey WHERE survey.fillInTime IS NULL OR survey.fillInTime >= :fillInTime")
    public List<Student> listStudent(@Variable("fillInTime") Date fillInTime);

    /**
     * 
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM SatisfactionSurvey ORDER BY createTime DESC,fillInTime DESC")
    public List<SatisfactionSurvey> list(@Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * 
     * @param site
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM SatisfactionSurvey WHERE courseRound.site = :site ORDER BY createTime DESC,fillInTime DESC")
    public List<SatisfactionSurvey> list(@Variable("site") Site site, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * 
     * @param sites
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM SatisfactionSurvey WHERE courseRound.site IN (:sites) ORDER BY createTime DESC,fillInTime DESC")
    public List<SatisfactionSurvey> list(@Variable("sites") List<Site> sites, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * 
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM SatisfactionSurvey WHERE fillInTime IS NOT NULL ORDER BY fillInTime DESC")
    public List<SatisfactionSurvey> listComplete(@Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * 
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM SatisfactionSurvey WHERE fillInTime IS NULL ORDER BY createTime DESC")
    public List<SatisfactionSurvey> listUncomplete(@Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * 
     * @param site
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM SatisfactionSurvey WHERE courseRound.site = :site AND fillInTime IS NOT NULL ORDER BY fillInTime DESC")
    public List<SatisfactionSurvey> listComplete(@Variable("site") Site site, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * 
     * @param site
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM SatisfactionSurvey WHERE courseRound.site = :site AND fillInTime IS NULL ORDER BY createTime DESC")
    public List<SatisfactionSurvey> listUncomplete(@Variable("site") Site site, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * 
     * @param sites
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM SatisfactionSurvey WHERE courseRound.site IN (:sites) AND fillInTime IS NULL ORDER BY createTime DESC")
    public List<SatisfactionSurvey> listUncomplete(@Variable("sites") List<Site> sites, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * 
     * @param sites
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM SatisfactionSurvey WHERE courseRound.site IN (:sites) AND fillInTime IS NOT NULL ORDER BY fillInTime DESC")
    public List<SatisfactionSurvey> listComplete(@Variable("sites") List<Site> sites, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * 
     * @param begin
     * @param end
     * @return
     */
    @Query("FROM SatisfactionSurvey WHERE fillInTime BETWEEN :begin AND :end ORDER BY fillInTime DESC")
    public List<SatisfactionSurvey> listComplete(@Variable("begin") Date begin, @Variable("end") Date end);

    /**
     * 
     * @param site
     * @param begin
     * @param end
     * @return
     */
    @Query("FROM SatisfactionSurvey WHERE courseRound.site = :site AND fillInTime BETWEEN :begin AND :end ORDER BY fillInTime DESC")
    public List<SatisfactionSurvey> listComplete(@Variable("site") Site site, @Variable("begin") Date begin, @Variable("end") Date end);

    /**
     * 
     * @param gift
     * @param used
     * @param begin
     * @param end
     * @return
     */
    @Query("FROM SatisfactionSurvey WHERE coupon.template.discountType = :gift AND coupon.status = :used AND coupon.useTime BETWEEN :begin AND :end")
    public List<SatisfactionSurvey> listUsedGiftCoupon(@Variable("gift") DiscountType gift, @Variable("used") CouponStatus used, @Variable("begin") Date begin, @Variable("end") Date end);

    /**
     * 
     * @param gift
     * @param used
     * @param site
     * @param begin
     * @param end
     * @return
     */
    @Query("FROM SatisfactionSurvey WHERE coupon.template.discountType = :gift AND coupon.status = :used AND courseRound.site = :site AND coupon.useTime BETWEEN :begin AND :end")
    public List<SatisfactionSurvey> listUsedGiftCoupon(@Variable("gift") DiscountType gift, @Variable("used") CouponStatus used, @Variable("site") Site site, @Variable("begin") Date begin, @Variable("end") Date end);

    /**
     * 
     * @param gift
     * @param used
     * @param sites
     * @param begin
     * @param end
     * @return
     */
    @Query("FROM SatisfactionSurvey WHERE coupon.template.discountType = :gift AND coupon.status = :used AND courseRound.site IN (:sites) AND coupon.useTime BETWEEN :begin AND :end")
    public List<SatisfactionSurvey> listUsedGiftCoupon(@Variable("gift") DiscountType gift, @Variable("used") CouponStatus used, @Variable("sites") List<Site> sites, @Variable("begin") Date begin, @Variable("end") Date end);

    /**
     * 
     * @param coupon
     * @return
     */
    @Query("FROM SatisfactionSurvey WHERE coupon = :coupon")
    public SatisfactionSurvey load(@Variable("coupon") Coupon coupon);

    /**
     * 
     * @param code
     * @param gift
     * @return
     */
    // @Query("FROM SatisfactionSurvey WHERE coupon.code = :code AND coupon.template.discountType = :gift")
    // public List<SatisfactionSurvey> list(@Variable("code") String code, @Variable("gift") DiscountType gift);
    /**
     * 
     * @param code
     * @return
     */
    @Query("FROM Coupon WHERE code like :code")
    public List<Coupon> list(@Variable("code") String code);
    
    
    @Query("FROM SatisfactionSurvey WHERE coupon in (:coupons)")
    public List<SatisfactionSurvey> list(@Variable("coupons") List<Coupon> coupons);
   
    /**
     * 
     * @param code
     * @return
     */
    @Query("FROM SatisfactionSurvey WHERE coupon.template.discountType = :gift")
    public List<SatisfactionSurvey> list(@Variable("gift") DiscountType gift);

    
}
